#include <linux/module.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
#include <linux/time.h>

 
static struct input_dev *input_dev;
static struct timer_list my_timer;
// static ulong jiffies = 30000;


static int val = 1;
void my_timer_function(struct timer_list * tl){
    val++;
    if(val==2){
        val = 0;
    }
    // printk("val=%d\n",val);
    input_event(input_dev, EV_KEY, KEY_1, val);
    input_sync(input_dev);
    // printk("timer function\n");
    mod_timer( &my_timer,  jiffies + msecs_to_jiffies(3000));
    // printk("timer input_report success\n");

}
static int __init myplaform_init(void){
    int ret;
    // init_timer(&my_timer);
    timer_setup(&my_timer,my_timer_function,0);
 
    input_dev = input_allocate_device();
    if(!input_dev){
        printk("input_alloc_device fail\n");
        return -ENOMEM;
    }
    input_dev->evbit[0] = BIT_MASK(EV_KEY);
    input_dev->name = "myinput";
    // input_dev->phys = "myinput/input";
    __set_bit(EV_KEY,input_dev->evbit);
    __set_bit(KEY_1,input_dev->keybit);

    ret = input_register_device(input_dev);
    if(ret < 0){
        printk("input_register_device fail\n");
        goto error;   
    }
 
    printk("input_register_device success\n");
    mod_timer( &my_timer, jiffies + msecs_to_jiffies(3000) );
    printk("timer init success\n");
    return 0;
error:
    input_free_device(input_dev);
    return ret;
}

static void __exit myplaform_exit(void){
    // input_free_device(input_dev);
    del_timer(&my_timer);
    input_unregister_device(input_dev);
    input_free_device(input_dev);
    
    printk("input_unregister_device success\n");
}
     

MODULE_AUTHOR("WLZ");
MODULE_LICENSE("GPL");
module_init(myplaform_init);
module_exit(myplaform_exit);